package com.hclc.entity.model.series;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.logging.log4j.util.Strings;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @Author: hef
 * 反序列化localdate
 * @Date: Create in 12:44 2019-10-04
 */
public class DeCustomLocalDateSerializer extends StdDeserializer<LocalDate> {
    protected DeCustomLocalDateSerializer() {
        super(LocalDate.class);
    }

    protected DeCustomLocalDateSerializer(Class<?> vc) {
        super(vc);
    }

    protected DeCustomLocalDateSerializer(JavaType valueType) {
        super(valueType);
    }

    protected DeCustomLocalDateSerializer(StdDeserializer<?> src) {
        super(src);
    }

    @Override
    public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String string = jsonParser.getText().trim();
        if (Strings.isBlank(string)) {
            return null;
        }

        LocalDate date = LocalDate.parse(string, df);
        return date;
    }
}
